<?php

class User_model extends CI_Model{

	//Gets the username of a given user id
	function getUsername($userid){
		$username;
		$this->db->select('username')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		$username = $row->username;
		return $username;
	}

	//Gets the user id of a given open id url
	function getUserId($openid_url){
		$userID;
		$this->db->select('userID')->from('user_openids')->where('openidURL',$openid_url);
		$query = $this->db->get();
		$row = $query->row();
		$userID = $row->userID;
		return $userID;
	}

	//Gets the public email address of a user
	function getPublicEmail($userid){
		$email = '-';
		$this->db->select('public_email')->from('user_openids')->where('userID', $userid);
		$query = $this->db->get();
		$row = $query->row();
		//Not public
		if($row->public_email == 0){
			return $email;
		}

		$email;
		$this->db->select('email')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		$email = $row->email;
		return $email;
	}
	
	//Gets the email address of a user regardless if it is private
	function getEmail($userid){
		$email;
		$this->db->select('email')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		$email = $row->email;
		return $email;
	}

	//Gets the open id url of a user
	function getPublicOpenId($userid){
		$openid = '-';
		$this->db->select('public_openid')->from('user_openids')->where('userID', $userid);
		$query = $this->db->get();
		$row = $query->row();
		//Not public
		if($row->public_openid == 0){
			return $openid;
		}

		$this->db->select('openidURL')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		$openid = $row->openidURL;
		return $openid;
	}

	//Gets the open id url of a user regardless if its private
	function getOpenId($userid){
		$this->db->select('openidURL')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		$openid = $row->openidURL;
		return $openid;
	}

	//Gets the user id that is stored in the session
	function getUserIdFromSession(){
		if(isset($this->session->userdata['userid']))
		return $this->session->userdata['userid'];
		return NULL;
	}
	
	//Updates the database a user credentials
	function updateSettings($userid, $username, $email, $public_openid, $public_email){
		$data = array('username'=>$username,'email'=>$email, 'public_openid'=>$public_openid, 'public_email'=>$public_email);
		$this->db->where('userID', $userid);
		$this->db->update('user_openids', $data);
	}

	//Returns if the email address of a user is public or not
	function isEmailPublic($userid){
		$this->db->select('public_email')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		return $row->public_email;
	}

	//Resturns if the open id url of a user is public or not
	function isOpenIdPublic($userid){
		$this->db->select('public_openid')->from('user_openids')->where('userID',$userid);
		$query = $this->db->get();
		$row = $query->row();
		return $row->public_openid;
	}
	
	//Checks of a user is logged in
	function isLoggedIn(){
		//TODO should check the session and the session database if the user is loggedin
		return (isset($this->session->userdata['logged_in']) && ($this->session->userdata['logged_in']==TRUE));
	}
	
	//Checks if a user is the correct session user.
	function sessionUser($userid){
		return ($userid == $this->getUserIdFromSession());
	}

}
